﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcGMaps.Models;
using System.Text;

namespace MvcGMaps.Utils
{
    public static class HtmlExtension
    {
        public static MvcHtmlString MarkerTypeSelect(this HtmlHelper html, string labelText, string htmlId, string htmlName, int id)
        {
           using(var context = new MarkersDBEntities())
           {
               StringBuilder builder = new StringBuilder();
               builder.Append("<div class=\"form-group input-group-sm\">");
               builder.Append(string.Format("<label class=\"control-label\" for=\"{0}\">{1}</label>", htmlId, labelText));
               builder.Append(string.Format("<select id=\"{0}\" class=\"form-control input-sm\" name=\"{1}\">", htmlId, htmlName));
               if (id == 0)
               {
                   builder.Append("<option value=0 selected>любой</option>");
               }

               var query = context.MarkerTypes.AsNoTracking().OrderBy(t => t.TypeID).ToList();
               
               for (int i = 0; i < query.Count; i++)
               {
                   string selected = id == query[i].TypeID ? " selected" : "";
                   string option = string.Format("<option value={0}{2}>{1}</option>", query[i].TypeID.ToString(), query[i].TypeName, selected);
                   builder.Append(option);
               }

               builder.Append("</select>");
               builder.Append("</div>");
               return MvcHtmlString.Create(builder.ToString());
           }
        }

        public static MvcHtmlString MarkerStatusSelect(this HtmlHelper html, string labelText, string htmlId, string htmlName, int id)
        {
            using (var context = new MarkersDBEntities())
            {
                StringBuilder builder = new StringBuilder();
                builder.Append("<div class=\"form-group input-group-sm\">");
                builder.Append(string.Format("<label class=\"control-label\" for=\"{0}\">{1}</label>", htmlId, labelText));
                builder.Append(string.Format("<select id=\"{0}\" class=\"form-control input-sm\" name=\"{1}\">", htmlId, htmlName));
                if (id == 0)
                {
                    builder.Append("<option value=0 selected>любой</option>");                    
                }

                var query = context.Statuses.AsNoTracking().OrderBy(s => s.StatusID).ToList();
                for (int i = 0; i < query.Count; i++)
                {
                    string selected = id == query[i].StatusID ? " selected" : "";
                    string option = string.Format("<option value={0}{2}>{1}</option>", query[i].StatusID.ToString(), query[i].StatusName, selected);
                    builder.Append(option);
                }

                builder.Append("</select>");
                builder.Append("</div>");

                return MvcHtmlString.Create(builder.ToString());
            }
        }

        public static MvcHtmlString InputDate(this HtmlHelper html, string labelText, string htmlId, string htmlName, string value, bool addJS)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("<div class=\"form-group\">");
            builder.AppendLine(string.Format("<label for=\"{0}\" class=\"control-label\">{1}</label>", htmlId, labelText));
            builder.AppendLine(string.Format("<div class=\"input-group date\" id=\"{0}\">", htmlId));
            builder.AppendLine(string.Format("<input type=\"text\" name=\"{0}\" class=\"form-control input-sm\" value=\"{1}\"/>", htmlName, value));
            builder.AppendLine("<span class=\"input-group-addon input-sm\"><span class=\"glyphicon glyphicon-calendar\"></span></span>");
            builder.AppendLine("</div>");
            if (addJS)
            {
                builder.AppendLine("<script type=\"text/javascript\">");
                //builder.AppendLine("$(function () {$('#" + htmlId + "').datepicker({format: 'dd.mm.yyyy', language: 'ru', todayBtn: 'linked', todayHighlight: true, autoclose: true});});");    
                builder.AppendLine("$(function () {$('div.date').datepicker({format: 'dd.mm.yyyy', language: 'ru', todayBtn: 'linked', todayHighlight: true, autoclose: true});});");    
            }
            builder.AppendLine("</script></div>");

            return MvcHtmlString.Create(builder.ToString());
        }
    }
}